package com.hy.study.practice.stack;

import java.util.Scanner;

/**
 * 使用链表模拟栈
 */
public class StackLinkedListDemo {
    public static void main(String[] args) {
        StackLinkedList stack = new StackLinkedList();
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        String key = "";
        while (loop) {
            System.out.println("--------please choose--------");
            System.out.println("show");
            System.out.println("push");
            System.out.println("pop");
            System.out.println("exit");
            key = scanner.next();
            switch (key) {
                case "show":
                    stack.list();
                    break;

                case "push":
                    System.out.println("please input number:");
                    int value = scanner.nextInt();
                    Node node = new Node(value, value + "");
                    stack.push(node);
                    break;
                case "pop":
                    try {
                        System.out.println("弹出元素:" + stack.pop());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    loop = false;
                    scanner.close();
                    break;
                default:
                    break;
            }
        }
        System.out.println("--------exit--------");
    }
}

/**
 * 使用单向链表模式栈
 */
class StackLinkedList {
    private Node head = new Node();

    public Node getHead() {
        return head;
    }

    public Node pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return null;
        }
        Node temp = head;
        boolean flag = false;
        while (true) {
            if (temp.getNextNode().getNextNode() == null) {
                flag = true;
                break;
            }
            temp = temp.getNextNode();
        }
        Node result=temp.getNextNode();
        temp.setNextNode(null);
        return result;
    }

    /**
     * 判断栈是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return head.getNextNode() == null;
    }

    /**
     * 将数据压入 到 栈中
     *
     * @param node
     */
    public void push(Node node) {
        boolean flag = false;
        Node temp = head;
        while (true) {
            if (temp.getNextNode() == null) {
                flag = true;
                break;
            }
            temp = temp.getNextNode();
        }
        if (flag) {
            temp.setNextNode(node);
        }
    }

    /**
     * 遍历整个栈 需要倒序遍历
     */
    public void list() {
        if (isEmpty()) {
            System.out.println("栈为空,无法遍历");
            return;
        }
        Node[] nodes = new Node[size()];
        int index = -1;
        Node temp = head.getNextNode();
        while (temp != null) {
            index++;
            nodes[index] = temp;
            temp = temp.getNextNode();
        }
        for (int i = index; i >= 0; i--) {
            System.out.println(nodes[i]);
        }


    }

    /**
     * 判断栈的大小
     *
     * @return
     */
    public int size() {
        int count = 0;
        Node temp = head.getNextNode();
        while (temp != null) {
            count++;
            temp = temp.getNextNode();
        }
        return count;

    }
}

class Node {
    private int no;
    private String name;
    private Node nextNode;

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public Node() {
    }

    public Node getNextNode() {
        return nextNode;
    }

    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}